<template>
  <div class="validate-form">
    <van-field
      class="personTop"
      left-icon="fail"
      value="请输入您的手机号和验证码进行认证"
      readonly
    />
    <van-form ref="identForm" class="validate" @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        :rules="rules.mobile"
        label="用户手机号"
        name="mobile"
        placeholder="输入手机号"
        type="number"
        maxlength="11"
      />
      <van-field
        v-model="user.code"
        :rules="rules.code"
        label="手机验证码"
        name="code"
        type="number"
        maxlength="6"
        placeholder="输入验证码"
      >
        <!-- 输入框组件Field => 插入按钮组件-->
        <template v-slot:button>
          <!-- 倒计时 :time="1000*5" format="ss 秒"-->
          <van-count-down
            v-if="isCountDownShow"
            @finish="isCountDownShow = false"
            :time="1000 * 60"
            format="ss 秒"
          />
          <van-button
            v-else
            @click="onSendSms"
            native-type="button"
            class="send-sms-btn"
            round
            size="small"
            type="default"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <van-button
        class="btnOK"
        type="info"
        color="#3F51B5"
        block
        native-type="submit"
      >
        提交认证
      </van-button>
    </van-form>
  </div>
</template>

<script>
import { identCheck, sendSms } from "@/api/ident";

export default {
  data() {
    return {
      user: {
        mobile: "",
        code: ""
      },
      rules: {
        mobile: [
          {
            required: true,
            message: "手机号不能为空"
          },
          {
            pattern: /^1[3|4|5|7|8]\d{9}$/,
            message: "手机号码格式错误"
          }
        ],
        code: [
          {
            required: true,
            message: "验证码不能为空"
          },
          {
            pattern: /^\d{6}$/,
            message: "验证码格式错误"
          }
        ]
      },
      isCountDownShow: false,
      uploader: []
    };
  },
  methods: {
    async onSubmit() {
      this.$toast.loading({
        message: "认证中...",
        forbidClick: true,
        duration: 0
      });
      try {
        const {
          data: { data }
        } = await identCheck(this.user);
        console.log(123);
        this.$store.commit("setUser", data);
        this.$toast.success("认证成功");
        this.$router.back();
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail("手机号或验证码错误");
        } else {
          this.$toast.fail("认证失败，请稍后重试");
        }
      }
    },
    async onSendSms() {
      // 1. 校验手机号
      try {
        await this.$refs.identForm.validate("mobile");
      } catch (err) {
        return console.log(err);
      }
      // 2. 校验通过 , 倒计时60s
      this.isCountDownShow = true;
      // 3. 请求发送验证码
      try {
        await sendSms(this.user.mobile);
        this.$toast("发送成功");
      } catch (err) {
        this.isCountDownShow = false;
        if (err.response.status === 429) {
          this.$toast("发送太频繁, 请稍后重试");
        } else {
          this.$toast("发送失败, 请稍后重试");
        }
      }
    }
  }
};
</script>

<style scoped lang="scss">
.validate-form {
  z-index: 2;
  position: absolute;
  top: 22vh;
  left: 5vw;
  width: 90%;
  height: 68vh;
  background-color: #fff;
  border-radius: 16px;
  ::v-deep.personTop {
    font-size: 28px;
    border-radius: 16px;
    .van-icon-fail {
      font-size: 40 px;
      &:before {
        color: #3f51b5;
      }
    }
  }

  .send-sms-btn {
    width: 160px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 24px;
    color: #666;
  }
  .btnOK {
    position: fixed;
    bottom: 0;
    left: 0;
    height: 100px;
  }
  .validate {
    padding: 0 40px;
    van-field {
      font-size: 40px;
    }
  }
}
</style>
